from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from news import models

class CateSer(serializers.ModelSerializer):
      class Meta:
        model = models.Cate
        fields = '__all__'

      def validate_name(self, value):
          menu = models.Cate.objects.filter(name=value)
          if '新闻' not in value:
              raise ValidationError("分类名必须携带新闻")
          if menu:
              raise ValidationError("名称不能重复")
          return value

class NewsSer(serializers.ModelSerializer):
      class Meta:
        model = models.News
        fields = '__all__'

      def validate_title(self, value):
          menu = models.News.objects.filter(title=value)
          if len(value) < 5:
              raise ValidationError("字符串长度必须在5个字符以上")
          if '美国' in value:
              raise ValidationError("标题不能携带美国")
          return value